package wmhsudoku.board;

import java.awt.Point;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

import wmhsudoku.gui.Main;

public class BruteForceSolver extends Solver {

	public BruteForceSolver(Board board) {
		super(board);
		try {
			out = new BufferedWriter(new FileWriter("BruteForceLog.txt"));
		} catch (IOException e) {
			e.printStackTrace();
		} 		
	}

	@Override
	public void solve() {
		if (!Main.RUNNING)
			return;
		
		for (int i=0; i<9; ++i) {
			for (int j=0; j<9; ++j) {
				++iterations;
				if (mask.getField(i,j) == 0)
					continue;
				boolean flag = true;
				int digit = sudoku.getField(i,j);
				do {
					++digit;
					if (digit > 9) {
						eMatrix.updateErrorMatrix(new Point(i,j), 0, sudoku.getField(i, j));
						sudoku.setField(i,j, 0);
						flag = false;
						do {
							--j;
							if (j < 0) {
								j=8;
								--i;
							}					
						} while (mask.getField(i,j) == 0);
						--j;
						break;
					}
					eMatrix.updateErrorMatrix(new Point(i,j), digit, sudoku.getField(i,j));
					sudoku.setField(i,j, digit);
					eMatrix.error();
				} while (eMatrix.getError() > 0);
				if (flag)
					sudoku.setField(i,j, digit);
			}
		}

		System.out.println("Liczba iteracji: " + iterations + "\n");
		try {
			out.write(iterations + "\n");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void init() {
		super.init();
	}

	@Override
	public boolean solveStep() {
		// TODO Auto-generated method stub
		return false;
	}

}
